import java.util.Comparator;
import java.util.List;

/**
 * @作者 zxy
 * @时间 2023-04-21 16:41
 * @说明 539. 最小时间差
 * 给定一个 24 小时制（小时:分钟 "HH:MM"）的时间列表，找出列表中任意两个时间的最小时间差并以分钟数表示。
 * 示例 1：
 * 输入：timePoints = ["23:59","00:00"]
 * 输出：1
 * 示例 2：
 * 输入：timePoints = ["00:00","23:59","00:00"]
 * 输出：0
 */
public class Solution {
    /**
     * 执行用时：3 ms, 在所有 Java 提交中击败了82.43%的用户
     * 内存消耗：41.1 MB, 在所有 Java 提交中击败了81.43%的用户
     * @param timePoints
     * @return
     */
    public int findMinDifference(List<String> timePoints) {
        if (timePoints.size() > 1440) {
            return 0;
        }
        timePoints.sort(Comparator.naturalOrder());
        int minDif = Integer.MAX_VALUE;
        for (int i = 0; i < timePoints.size(); i++) {
            if (i != timePoints.size() - 1) {
                String s1 = timePoints.get(i);
                String s2 = timePoints.get(i + 1);
                int i1 = Integer.parseInt(s1.substring(0, 2)) * 60 + Integer.parseInt(s1.substring(3, 5));
                int i2 = Integer.parseInt(s2.substring(0, 2)) * 60 + Integer.parseInt(s2.substring(3, 5));
                if (minDif > i2 - i1) {
                    minDif = i2 - i1;
                }
            } else {
                String s1 = timePoints.get(i);
                String s2 = timePoints.get(0);
                int i1 = Integer.parseInt(s1.substring(0, 2)) * 60 + Integer.parseInt(s1.substring(3, 5));
                int i2 = Integer.parseInt(s2.substring(0, 2)) * 60 + Integer.parseInt(s2.substring(3, 5));
                if (minDif > i2 + 24 * 60 - i1) {
                    minDif = i2 + 24 * 60 - i1;
                }
            }
        }

        return minDif;
    }
}
